import random
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("http://localhost/ranzhi/www")

#定位输入框输入用户名root
#<input type="text" name="account" id="account" value="" class="form-control" placeholder="请输入用户名">
driver.find_element(By.ID,"account").send_keys("root")
#定位密码框,输入密码123456
#<input type="password" name="password" id="password" value="" class="form-control" placeholder="请输入密码">
driver.find_element(By.ID,"password").send_keys("123456")
#点击登录
#<button type="submit" id="submit" class="btn btn-primary" data-loading="稍候...">登录</button>
driver.find_element(By.ID,"submit").click()
sleep(3)

#点击客户管理
#<li id="s-menu-1" data-order="1" class=""><button data-toggle="tooltip" data-tip-class="s-menu-tooltip" data-placement="right" data-btn-type="menu" class="app-btn s-menu-btn open" title="" data-id="1" data-original-title="客户管理"><img src="/ranzhi/www/theme/default/images/ips/app-crm.png" alt=""></button></li>
driver.find_element(By.ID, "s-menu-1").click()
sleep(2)
#进入<iframe
#<iframe id="iframe-1" name="iframe-1" src="/ranzhi/www/sys/entry-visit-1.html" frameborder="no" allowtransparency="true" scrolling="auto" hidefocus="" style="width: 100%; height: 100%; left: 0px;"></iframe>
a = driver.find_element(By.ID,"iframe-1")
driver.switch_to.frame(a)
#点击联系人
driver.find_element(By.LINK_TEXT,"联系人").click()
sleep(1)
#点击添加联系人
driver.find_element(By.LINK_TEXT,"添加联系人").click()
sleep(1)

#生成随机人物信息
import faker
#创建一个faker对象，指定生成测试数据语言
f = faker.Faker(locale="zh-CN")
#生成一个虚拟用户的个人资料
un = f.profile()
#随机生成一个手机号
phnu = f.phone_number()
################################################################################
# for i in range(9):
#     n = random.choice([1, 2, 3, 4, 5, 6, 7, 8])
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[{n}]').click()
###############################################################################
#选择真实姓名框
#<input type="text" name="realname" id="realname" value="" class="form-control">
driver.find_element(By.ID,"realname").send_keys(f"{un.get("name")}")
#点击新建所属客户
#<input type="checkbox" name="newCustomer" id="newCustomer" value="1">
driver.find_element(By.ID,"newCustomer").click()
#添加所属客户
#<input type="text" name="name" id="name" value="" class="form-control" style="display: block;">
driver.find_element(By.ID,"name").send_keys(f"{un.get("username")}")
#选择性别
#<input type="radio" name="gender" value="m" id="genderm">
#<input type="radio" name="gender" value="f" id="genderf">
if un.get("sex") == "M":
    driver.find_element(By.ID, "genderm").click()
else:
    driver.find_element(By.ID, "genderf").click()
#填写部门
#<input type="text" name="dept" id="dept" value="" class="form-control">
driver.find_element(By.ID, "dept").send_keys(f"{un.get("company")}")
#填写职位
#<input type="text" name="title" id="title" value="" class="form-control">
driver.find_element(By.ID, "title").send_keys(f"{un.get("job")}")
#填写入职时间
#<input type="text" name="join" id="join" value="" class="form-control">
driver.find_element(By.ID, "join").send_keys(f"{un.get("birthdate")}")
#邮箱
#<input type="text" name="email" id="email" value="" class="form-control" placeholder="多个邮箱之间用逗号或者空格隔开">
driver.find_element(By.ID, "email").send_keys(f"{un.get("mail")}")
#手机号
#<input type="text" name="mobile" id="mobile" value="" class="form-control">
driver.find_element(By.ID, "mobile").send_keys(f"{phnu}")
#电话
#<input type="text" name="phone" id="phone" value="" class="form-control">
driver.find_element(By.ID, "phone").send_keys(f"{phnu}")
#传真
#<input type="text" name="fax" id="fax" value="" class="form-control">
from faker import Faker
faker = Faker()
# 生成5位整数，不足位时补前导0（如生成"00372"）
num1 = faker.random_number(digits=11, fix_len=True)
num2 = faker.random_number(digits=12, fix_len=True)
num3 = faker.random_digit()
driver.find_element(By.ID, "fax").send_keys(f"{num1}")
#QQ
#<input type="text" name="qq" id="qq" value="" class="form-control">
driver.find_element(By.ID, "qq").send_keys(f"{num2}")
#类型
#<select name="type" id="type" class="form-control">//*[@id="type"]
driver.find_element(By.ID, "type").click()
# if num3 == 1:#//*[@id="type"]/option[1]
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[1]').click()
# elif num3 == 2:#//*[@id="type"]/option[2]
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[2]').click()
# elif num3 == 3:#//*[@id="type"]/option[4]//*[@id="type"]/option[3]
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[3]').click()
# elif num3 == 4:#//*[@id="type"]/option[5]
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[4]').click()
# elif num3 == 5:#//*[@id="type"]/option[6]
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[5]').click()
# elif num3 == 6:#//*[@id="type"]/option[7]
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[6]').click()
# elif num3 == 7:#//*[@id="type"]/option[8]
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[7]').click()
# else:
#     driver.find_element(By.XPATH, f'//*[@id="type"]/option[8]').click()
n = random.choice([1, 2, 3, 4, 5, 6, 7, 8])
driver.find_element(By.XPATH, f'//*[@id="type"]/option[{n}]').click()

#规模
#<select name="size" id="size" class="form-control">
driver.find_element(By.ID, "type").click()
n = random.choice([1, 2, 3, 4, 5])
driver.find_element(By.XPATH, f'//*[@id="size"]/option[{n}]').click()
# if num3 == 1:#//*[@id="size"]/option[1]
#     driver.find_element(By.XPATH, f'//*[@id="size"]/option[1]').click()
# elif num3 == 2:#//*[@id="size"]/option[2]
#     driver.find_element(By.XPATH, f'//*[@id="size"]/option[2]').click()
# elif num3 == 3:#//*[@id="size"]/option[3]
#     driver.find_element(By.XPATH, f'//*[@id="size"]/option[3]').click()
# elif num3 == 4:#//*[@id="size"]/option[4]
#     driver.find_element(By.XPATH, f'//*[@id="size"]/option[5]').click()
# else:#//*[@id="size"]/option[5]
#     driver.find_element(By.XPATH, f'//*[@id="size"]/option[4]').click()
#状态
#<select name="status" id="status" class="form-control">
driver.find_element(By.ID, "status").click()
n = random.choice([1, 2, 3, 4, 5])
driver.find_element(By.XPATH, f'//*[@id="status"]/option[{n}]').click()
# #//*[@id="status"]/option[1]
# if num3 == 3:
#     driver.find_element(By.XPATH, f'//*[@id="status"]/option[1]').click()
# elif num3 == 1:
#     driver.find_element(By.XPATH, f'//*[@id="status"]/option[2]').click()
# elif num3 == 4:
#     driver.find_element(By.XPATH, f'//*[@id="status"]/option[3]').click()
# elif num3 == 2:
#     driver.find_element(By.XPATH, f'//*[@id="status"]/option[5]').click()
# else:
#     driver.find_element(By.XPATH, f'//*[@id="status"]/option[4]').click()
#级别
#<select name="level" id="level" class="form-control">
driver.find_element(By.ID, "level").click()
#<option value="B">B(有明显的业务需求，预计三个月内成交)</option>
n = random.choice([1, 2, 3, 4, 5, 6])
driver.find_element(By.XPATH, f'//*[@id="level"]/option[{n}]').click()
# if num3 == 5:#//*[@id="size"]/option[1]
#     driver.find_element(By.XPATH, f'//*[@id="level"]/option[1]').click()
# elif num3 == 3:#//*[@id="level"]/option[2]
#     driver.find_element(By.XPATH, f'//*[@id="level"]/option[2]').click()
# elif num3 == 2:#//*[@id="level"]/option[3]
#     driver.find_element(By.XPATH, f'//*[@id="level"]/option[3]').click()
# elif num3 == 6:#//*[@id="size"]/option[4]
#     driver.find_element(By.XPATH, f'//*[@id="level"]/option[4]').click()
# elif num3 == 7:
#     driver.find_element(By.XPATH, f'//*[@id="level"]/option[5]').click()
# else:#//*[@id="level"]/option[6]
#     driver.find_element(By.XPATH, f'//*[@id="level"]/option[6]').click()
#简介
#<textarea name="desc" id="desc" rows="3" class="form-control"></textarea>
driver.find_element(By.ID, "desc").send_keys(f"这是一个简介，我也不知道写什么，但是它空着，那应该是要写一些什么进去的")
#提交
#<button type="submit" id="submit" class="btn btn-primary" data-loading="稍候..." value="">保存</button>
driver.find_element(By.ID, "submit").click()
sleep(5)







# #输入用户名
# #
# driver.find_element(By.ID,"account").send_keys()
# #输入真名
# #<input type="text" name="realname" id="realname" value="" class="form-control" autocomplete="off">
# driver.find_element(By.ID,"realname").send_keys(f"{un.get("name")}")
# #
# sleep(3)